package com.by.common.exception;

import com.by.common.bean.ResultBean;
import com.by.controller.BaseController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalJsonException extends BaseController {

    @ExceptionHandler(Exception.class)
    public Map handlerException(Exception e){
        Map map=new HashMap();
        map.put("code","0");
        map.put("msg",e.getMessage());
        e.printStackTrace();
        return map;
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResultBean handlerUploadExceedMaxSizeException(MaxUploadSizeExceededException e){
        long max=e.getMaxUploadSize();
        return fail("文件上传失败,超出最大文件限制["+max+"]byte");
    }

    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidateException(ValidateException e){
        return fail("验证失败",e.getErrorData());
    }
}
